import numpy as np
import cv2
from answer1.Boxes import BoxItem
class ImgBoxes:
    def __init__(self, box: BoxItem, img: np.ndarray):
        self.box = box
        self.img = img

    def construct(self):
        # check the box is legal for the image.
        if self.box.W > self.img.shape[0] or self.box.H > self.img.shape[1]:
            raise ValueError("Wrong box size %d X %d for the image with shape %d X %d"
                             "" %(self.box.W, self.box.H, self.img.shape[0], self.img.shape[1]))

    # 要求2：填充图像
    def __add__(self, other_img):
        """
        This function aims to overlap an image on the self.img with size and location of self.box.
        :param other_img: An image to be overlapped on self.img.
        :return: Overlapped image.
        """
        # resize the image accord the box size.
        other_img = cv2.resize(other_img.img, (self.box.H, self.box.W))
        # compute ranges of the overlap area in target image.
        wide_start, wide_end = self.box.left_top[0], self.box.right_bottom[0]
        height_start, height_end = self.box.left_top[1], self.box.right_bottom[1]
        # cover the other_img on the target image.
        image = self.img.copy()
        image[wide_start:wide_end, height_start:height_end, :] = other_img
        return ImgBoxes(self.box, image)

